$(function() {
	var countCache = new Cache(100);// 缓存注数计算结果
	var hashObj = {};// 存放选择的选项
	var seletedMatchCount = 0; // 当前选中的场次数
	var MAX_BETUNITS = 10000;
	var MAX_BETUNITS_ERROR_MSG = '单倍注数不能超过' + MAX_BETUNITS + '注。';
	var selected_class = 'dcchose';

	if (maxPassMatchCount == null) {
		alert('本玩法最大的过关场次数未设置！');
		return;
	}
	
	function handleCheckedOfTd(tdEl,checked){
		if (checked) {
			$(tdEl).addClass(selected_class); 
		} else {
			$(tdEl).removeClass(selected_class); 
		}
	}

	var updateBetUnits_parent = updateBetUnits;
	window.updateBetUnits = function(units) {
		var msgEl = document.getElementById('span_msg');
		var errEl = document.getElementById('span_err');
		if (units == MAX_BETUNITS_ERROR_MSG) {
			errEl.innerHTML = MAX_BETUNITS_ERROR_MSG;
			msgEl.style.display = 'none';
			errEl.style.display = '';
			updateBetUnits_parent(0);
		} else {
			errEl.style.display = 'none';
			errEl.innerHTML = '';
			msgEl.style.display = '';
			updateBetUnits_parent(units);
		}
	};

	function hashObj_push(matchKey, obj) {
		hashObj[matchKey] = obj;
		seletedMatchCount++;
	}
	function hashObj_remove(matchKey) {
		delete hashObj[matchKey];
		seletedMatchCount--;
	}

	window.clickItem = function(matchKey, itemIndex, event) {
		clickItemFn(matchKey, itemIndex, event);
		displayOption();
		countBetUnits();
	};

	function clickItemFn(matchKey, itemIndex, event) {
		var chkEl = document.getElementById('chk_' + matchKey + '_' + itemIndex);
		if (event != null) {
			Event_stopPropagation(event);// event.stopPropagation();
			var target = Event_target(event);// event.target
			if (target.id != chkEl.id)
				chkEl.checked = !chkEl.checked;
		} else {
			chkEl.checked = !chkEl.checked;
		}

		var tdEl = document.getElementById('td_' + matchKey + '_' + itemIndex);
		handleCheckedOfTd(tdEl, chkEl.checked);
		handleItemChg(matchKey, itemIndex, chkEl.checked);
	}

	function handleItemChg(matchKey, itemIndex, checked) {
		var r = handleItemChgFn(matchKey, itemIndex, checked);
		if (matchKey in hashObj) {
			var obj = hashObj[matchKey];
			for ( var i = 0; i < rowItemValues.length; i++) {
				var rowVal = rowItemValues[i];
				document.getElementById('bao_' + i + '_' + matchKey).checked = (obj.value & rowVal) == rowVal;
			}
		} else {
			for ( var i = 0; i < rowItemValues.length; i++) {
				document.getElementById('bao_' + i + '_' + matchKey).checked = false;
			}
		}
		if (r != 0) {
			// 清空设胆
			for(var matchKey in hashObj){
				var obj = hashObj[matchKey];
				if (obj != null && obj.dan == true)
					obj.dan = false;
			}

			displayPassType();
		}
	}

	/**
	 * 处理选项变化的函数
	 * 
	 * @param matchKey 场次序号
	 * @param itemIndex 选项序号
	 * @param checked 是否选中
	 * @return 1表示选中场次增加,-1表示选中场次减少,0表示选中场次数目无变化
	 */
	function handleItemChgFn(matchKey, itemIndex, checked) {
		if (matchKey in hashObj) {
			var obj = hashObj[matchKey];
			var v = 1 << itemIndex;
			if (checked == true) {
				obj.value |= v;
				obj.count += 1;
				return 0;
			} else {
				obj.value ^= v;
				if (obj.value == 0) {
					hashObj_remove(matchKey);
					return -1;
				} else {
					obj.count -= 1;
					return 0;
				}
			}

		} else if (checked == true) {
			hashObj_push(matchKey, {
				matchKey : matchKey,
				matchText : document.getElementById('text_' + matchKey).innerHTML,
				home : document.getElementById('td_h_' + matchKey).innerHTML,
				guest : document.getElementById('td_g_' + matchKey).innerHTML,
				value : 1 << itemIndex,
				count : 1,
				dan : false
			});
			return 1;
		}
	}

	window.deleteLine = function(matchKey) {
		var obj = hashObj[matchKey];
		var k = 0;
		for ( var p in PlayItem) {
			if ((obj.value & (1 << k)) > 0) {
				document.getElementById('chk_' + matchKey + '_' + k).checked = false;
				var tdEl = document.getElementById('td_' + matchKey + '_' + k);
				tdEl.bgColor = '';
				tdEl.className = '';
			}
			k++;
		}
		for ( var i = 0; i < rowItemValues.length; i++) {
			document.getElementById('bao_' + i + '_' + matchKey).checked = false;
		}
		hashObj_remove(matchKey);
		updateBetUnits(0);
		displayOption();
		displayPassType();
	};

	window.rowBatch = function(matchKey, rowIndex, event) {
		var baoEl = document.getElementById('bao_' + rowIndex + '_' + matchKey);
		if (event != null) {
			var target = Event_target(event);// event.target
			if (target.id != baoEl.id)
				baoEl.checked = !baoEl.checked;
		}
		var rowVal = rowItemValues[rowIndex];
		rowSelected(matchKey, rowVal, baoEl.checked);
	};

	window.halfAndFullSelect = function(matchKey) {
		var fullVal = document.getElementById('full_select_' + matchKey).value;
		var halfVal = document.getElementById('half_select_' + matchKey).value;
		var rowVal = fullVal & halfVal;
		;
		var k = 0;
		var chgCount = 0;
		for ( var p in PlayItem) {
			var checked = (rowVal & (1 << k)) > 0;
			var chkEl = document.getElementById('chk_' + matchKey + '_' + k);
			if (chkEl.checked != checked) {
				chkEl.checked = checked;
				var tdEl = document.getElementById('td_' + matchKey + '_' + k);
				handleCheckedOfTd(tdEl, checked);
				var r = handleItemChgFn(matchKey, k, checked);
				chgCount += r;
			}
			k++;
		}
		displayOption();
		if (chgCount != 0) {
			updateBetUnits(0);
			displayPassType();
		} else {
			countBetUnits();
		}
	};

	function rowSelected(matchKey, rowVal, checked) {
		var k = 0;
		var chgCount = 0;
		for ( var p in PlayItem) {
			if ((rowVal & (1 << k)) > 0) {
				var chkEl = document.getElementById('chk_' + matchKey + '_' + k);
				if (chkEl.checked != checked) {
					document.getElementById('chk_' + matchKey + '_' + k).checked = checked;
					var tdEl = document.getElementById('td_' + matchKey + '_' + k);
					handleCheckedOfTd(tdEl, checked);
					var r = handleItemChgFn(matchKey, k, checked);
					chgCount += r;
				}
			}
			k++;
		}
		displayOption();
		if (chgCount != 0) {
			updateBetUnits(0);
			displayPassType();
		} else {
			countBetUnits();
		}
	}

	window.columnBatch = function(itemIndex, event) {
		var columnEl = document.getElementById('column_' + itemIndex);
		if (event != null) {
			var target = Event_target(event);// event.target
			if (target.id != columnEl.id)
				columnEl.checked = !columnEl.checked;
		}
		var checked = columnEl.checked;
		var matchTbody = document.getElementById('matchTbody');
		var rows = matchTbody.rows;
		var chgCount = 0;
		for ( var i = 1, len = rows.length; i < len; i++) {
			var row = rows[i];
			if (row.getAttribute('_m') != null && row.getAttribute('_e') == '0' && row.style.display != 'none') {
				var matchKey = parseInt(row.getAttribute('_lid'), 10);
				var chkEl = document.getElementById('chk_' + matchKey + '_' + itemIndex);
				if (chkEl.checked != checked) {
					document.getElementById('chk_' + matchKey + '_' + itemIndex).checked = checked;
					var tdEl = document.getElementById('td_' + matchKey + '_' + itemIndex);
					handleCheckedOfTd(tdEl, checked);
					var r = handleItemChgFn(matchKey, itemIndex, checked);
					chgCount += r;
				}
			}
		}
		displayOption();
		if (chgCount != 0) {
			updateBetUnits(0);
			displayPassType();
		} else {
			countBetUnits();
		}
	};

	function getItemHtml(obj, index, canSetDan, canAddDan) {
		var danInputStr;
		if (canSetDan) {
			styleStr = '';
			if (obj.dan)
				danInputStr = 'checked="checked"';
			else
				danInputStr = canAddDan ? '' : 'disabled="disabled"';
		} else {
			styleStr = 'style="display:none"';
			danInputStr = 'disabled="disabled"';
		}

		var td_reduced = document.getElementById("td_reduced_"+obj.matchKey);
		
		var html = '';
		html += '<tr class="trw" height="24">';
		html += '  <td><input type="checkbox" checked="checked" onclick="deleteLine(\''
				+ obj.matchKey + '\')">' + obj.matchText + '</td>';
		html += '  <td>' + obj.home + '<br/>vs<br/>' + obj.guest + '</td>';
		if(td_reduced != null){
			html += '		<td>'+td_reduced.innerHTML+'</td>';
		}
		html += '  <td><input type="hidden" name="createForm.items[' + index
				+ '].matchKey" value="' + obj.matchKey + '" /><input type="hidden" name="createForm.items[' + index
				+ '].value" value="' + obj.value + '" />';
		var k = 0;
		var valueIndex = 0;
		var spStr = '';
		for ( var p in PlayItem) {
			if ((obj.value & (1 << k)) > 0) {
				var item = PlayItem[p];
				html += '	 <span style="display:inline-block"><input type="checkbox" checked="checked" onclick="clickItem(\'' + obj.matchKey + '\',' + k
						+ ',event);" />' + item.text + '</span>';
				;
				var spSpan = document.getElementById('sp_' + obj.matchKey + '_' + k);
				var sp;
				if (spSpan != null)
					sp = spSpan.innerHTML;
				else
					sp = 0.00;
				if (spStr != '')
					spStr += ',';
				spStr += sp;
				valueIndex++;
			}
			k++;
		}
		html += '<input type="hidden" name="spss[' + index + ']" value="' + spStr + '" />';
		html += '  </td>';
		html += '  <td class="border_top" ' + styleStr + '><input _matchKey="' + obj.matchKey + '" type="checkbox" '
				+ danInputStr + 'name="createForm.items[' + index
				+ '].dan" value="true" onclick="clickShedan(this);" /></td>';
		html += '</tr>';
		return html;
	}

	function displayOption() {
		var canSetDan = isMultiplePass();
		var canAddDan = false;//
		
		var td_reduced = document.getElementById("td_reduced");
		
		var html = '';
		html += '<table width="390" cellspacing="1" cellpadding="0" border="0" class="dctbg">';
		html += '  <tbody>';
		html += '    <tr height="22" class="dctro">';
		html += '      <td height="27" colspan="'+((td_reduced != null)?5:4)+'">场次及投注选项</td>';
		html += '    </tr>';
		html += '    <tr height="22" class=" dctry">';
		html += '		<td width="65">场次 </td>';
		html += '		<td width="120">比赛 </td>';
		if(td_reduced != null){
			html += '		<td width="'+td_reduced.width+'">'+td_reduced.innerHTML+'</td>';
		}
		html += '		<td>您的投注</td>';
		html += '		<td id="td_danma" ' + (canSetDan ? '' : 'style="display:none"') + '>胆码</td>';
		html += '	  </tr>';
		var index = 0;
		for(var matchKey in hashObj){
			var obj = hashObj[matchKey];
			if (obj != null) {
				html += getItemHtml(obj, index, canSetDan, canAddDan);
				index++;
			}
		}
		html += '  </tbody>';
		html += '</table>';
		document.getElementById('div_op').innerHTML = html;
	}

	function isMultiplePass() {
		var obj = document.getElementById('createForm_schemeType_2');
		return obj != null && obj.checked;
	}

	function isSinglePass() {
		var obj = document.getElementById('createForm_schemeType_0');
		return obj != null && obj.checked;
	}

	window.clickShedan = function(el) {
		var matchKey = el.getAttribute('_matchKey');
		var obj = hashObj[matchKey];
		obj.dan = el.checked;
		updateShedan();
		countBetUnits();
	};

	/**
	 * 更新设胆和模糊设胆
	 */
	function updateShedan() {
		// 更新设胆和模糊设胆
		var createForm = getCreateForm();
		var minPassType;// 最小的过关方式
		var passTypeCount = 0;// 过关方式数目
		var arr = createForm.elements['createForm.passTypes'];
		if (arr != null) {
			if (arr.length == null) {
				arr = [ arr ];
			}
			for ( var i = 0, len = arr.length; i < len; i++) {
				var typeEl = arr[i];
				if (typeEl.checked) {
					var type = PassType[typeEl.value];
					if (minPassType == null)
						minPassType = type;
					passTypeCount++;
				}
			}
		}

		var canShedanNum = 0; // 可以设胆的数目
		if (minPassType != null) {
			if (seletedMatchCount > minPassType.matchCount) {
				canShedanNum = minPassType.matchCount;
				if (passTypeCount == 1) {
					canShedanNum--;
				}
			}
		}

		var danSize = 0; // 胆码数目
		var unDanSize = 0; // 非胆码数目
		if (canShedanNum < 1) { // 可设胆数目小于1，即不能设胆，所有设胆置为不可用
			for ( var i = 0; i < seletedMatchCount; i++) {
				var danmaEl = createForm.elements['createForm.items[' + i + '].dan'];
				danmaEl.checked = false;
				danmaEl.disabled = true;
			}
		    ////把所有都设为非胆
			for(var matchKey in hashObj){
				var obj = hashObj[matchKey];
				if (obj != null && obj.dan == true)
					obj.dan = false;
			}
		} else {
			var danArr = [];
			var unDanArr = [];
			for ( var i = 0; i < seletedMatchCount; i++) {
				var danmaEl = createForm.elements['createForm.items[' + i + '].dan'];
				if (danmaEl.checked) {
					danArr.push(danmaEl);
				} else {
					unDanArr.push(danmaEl);
				}
			}
			danSize = danArr.length;
			unDanSize = unDanArr.length;

			if (danSize > canShedanNum) {// 当前设胆数目大于可以设胆的数目，取消所有设胆，其他不可用设胆的场次重置为可用
				for ( var i = 0; i < danArr.length; i++) {
					danArr[i].checked = false;
				}
				for ( var i = 0; i < unDanArr.length; i++) {
					unDanArr[i].disabled = false;
				}
				unDanSize += danSize;
				danSize = 0;
			} else if (danSize == canShedanNum) {// 当前设胆数目等于可以设胆的数目，把其他未设胆的场次的设胆置为不可用
				for ( var i = 0; i < unDanArr.length; i++) {
					unDanArr[i].disabled = true;
				}
			} else {// 当前设胆数目小于可以设胆的数目，把其他未设胆的场次的设胆置为可用
				for ( var i = 0; i < unDanArr.length; i++) {
					unDanArr[i].disabled = false;
				}
			}
		}

		// 更新模糊设胆
		var html = '';
		if (minPassType != null && danSize > 0) {
			var danMinHit = minPassType.matchCount - unDanSize;
			if (danMinHit < 1)
				danMinHit = 1;
			var danMaxHit = danSize;
			html += '<select id="createForm_danMinHit" name="createForm.danMinHit" onchange="chgMohuSheDan(this);">';
			for ( var i = danMaxHit; i >= danMinHit; i--) {
				html += '<option value=' + i + '>至少对' + i + '场</option>';
			}
			html += '</select>';
		}
		document.getElementById('mohushedan').innerHTML = html;
	}

	/**
	 * 切换模糊设胆
	 */
	window.chgMohuSheDan = function(obj) {
		countBetUnits();
	};

	/**
	 * 切换过关模式
	 */
	window.chgPassMode = function() {
		var multPass = isMultiplePass();
		var singlePass = isSinglePass();
		var fn;
		var danmaHeadEl = document.getElementById('td_danma');
		if (!multPass) {
			document.getElementById('div_mohushedan').style.display = 'none';
			document.getElementById('mohushedan').innerHTML = '';

			var index = 0;
			for(var matchKey in hashObj){
				var obj = hashObj[matchKey];
				if (obj != null)
					obj.dan = false;
			}
			fn = function(el) {
				el.checked = false;
				el.disabled = true;
				el.parentNode.style.display = 'none';
			};
			danmaHeadEl.style.display = 'none';
		} else {
			document.getElementById('div_mohushedan').style.display = '';

			fn = function(el) {
				el.checked = false;
				el.disabled = true;
				el.parentNode.style.display = '';
			};
			danmaHeadEl.style.display = '';
		}
		var createForm = getCreateForm();
		for ( var i = 0; i < seletedMatchCount; i++) {
			var danmaEl = createForm.elements['createForm.items[' + i + '].dan'];
			fn(danmaEl);
		}
		if(!singlePass)
			updateBetUnits(0);
		displayPassType();
	};

	/**
	 * 切换过关方式
	 * 
	 * @param obj
	 */
	window.chgPassType = function(obj) {
		var multPass = isMultiplePass();
		if (multPass) {
			updateShedan();
		}
		countBetUnits();
	};

	/**
	 * 更新过关方式显示
	 */
	function displayPassType() {
		var el = document.getElementById('pass_container');
		if (seletedMatchCount == 0) {
			el.innerHTML = '&nbsp;';
			return;
		}

		document.getElementById('mohushedan').innerHTML = '';// 清空模糊设胆

		var multPass = isMultiplePass();
		var singlePass = isSinglePass();
		var typeArr;
		var inputType;
		if (multPass) {
			typeArr = PassTypeUtil.getMultiplePassType((seletedMatchCount > maxPassMatchCount) ? maxPassMatchCount
					: seletedMatchCount);
			inputType = 'checkbox';
		} else if(singlePass){
			if (seletedMatchCount > window.maxSingleMatchCount) {
				el.innerHTML = '<font color="red">单关最多只能选择' + window.maxSingleMatchCount + '场赛事</font>';
				return;
			}
			var type = PassTypeUtil.getSinglePassType(seletedMatchCount);
			el.innerHTML = '<input type="radio" checked="checked" style="display:none" name="createForm.passTypes" value="' + type.key + '" /><span style="color:red;font-weight:bold">'+ type.text + '</span>';
			countBetUnits();
			return;
		} else {
			if (seletedMatchCount > window.maxSingleMatchCount) {
				el.innerHTML = '<font color="red">最多只能选择' + window.maxSingleMatchCount + '场赛事</font>';
				return;
			}
			inputType = 'radio';
			typeArr = PassTypeUtil.getNormalPassType(seletedMatchCount);
		}

		var html = '';
		for ( var i = 0, len = typeArr.length; i < len; i++) {
			var type = typeArr[i];
			var inputId = 'passType_' + type.key;
			html += '<input type="' + inputType + '" id="' + inputId + '" value="' + type.key
					+ '" name="createForm.passTypes" onclick="chgPassType();"><label for="' + inputId + '">'
					+ type.text + '</label>&nbsp;&nbsp;';
		}
		el.innerHTML = html;
	}

	function countBetUnits() {
		var multPass = isMultiplePass();
		if (multPass)
			countMultiplePassUnits();
		else
			countNormalPassUnits();
	}

	/**
	 * 普通过关计算注数
	 * 
	 */
	function countNormalPassUnits() {
		var type;// 当前的过关方式
		var createForm = getCreateForm();
		var typeArr = createForm.elements['createForm.passTypes'];
		if (typeArr != null) {
			if (typeArr.length == null) {
				typeArr = [ typeArr ];
			}
			for ( var i = 0, len = typeArr.length; i < len; i++) {
				var typeEl = typeArr[i];
				if (typeEl.checked) {
					type = PassType[typeEl.value];
					break;
				}
			}
		}

		var betUnits = 0;
		if (type != null) {
			try {
				var bets = [];
				for(var matchKey in hashObj){
					var obj = hashObj[matchKey];
					if (obj != null) {
						bets.push(obj.count);
					}
				}
				bets.sort();
				for ( var i = 0, len = type.passMatchs.length; i < len; i++) {
					betUnits += simpleCountFn(bets, type.passMatchs[i]);
					if (betUnits > MAX_BETUNITS) {
						throw MAX_BETUNITS_ERROR_MSG;
					}
				}
			} catch (e) {
				if (e == MAX_BETUNITS_ERROR_MSG) {
					updateBetUnits(e);
					return;
				}
			}
		}
		updateBetUnits(betUnits);
	}

	function countMultiplePassUnits() {
		var betUnits = 0;

		var danBets = [];
		var unDanBets = [];
		for(var matchKey in hashObj){
			var obj = hashObj[matchKey];
			if (obj != null) {
				if (obj.dan)
					danBets.push(obj.count);
				else
					unDanBets.push(obj.count);
			}
		}
		danBets.sort();
		unDanBets.sort();
		var danSize = danBets.length;

		var createForm = getCreateForm();
		var typeArr = createForm.elements['createForm.passTypes'];
		if (typeArr != null) {
			if (typeArr.length == null) {
				typeArr = [ typeArr ];
			}
			try {
				if (danSize > 0) {
					var danMinEl = createForm.elements['createForm.danMinHit'];
					var danMinHit;// 胆码最小命中
					if (danMinEl != null)
						danMinHit = parseInt(danMinEl.value, 10);
					else
						danMinHit = danSize;

					var danMaxHit = danSize;// TODO:胆码最大命中
					for ( var i = 0, l1 = typeArr.length; i < l1; i++) {
						var typeEl = typeArr[i];
						if (typeEl.checked) {
							var type = PassType[typeEl.value];
							for ( var j = 0, l2 = type.passMatchs.length; j < l2; j++) {
								var needs = type.passMatchs[j];
								for ( var danHit = danMinHit; danHit <= danMaxHit; danHit++) {
									betUnits += shedanCountFn(danBets, unDanBets, danHit, needs);
									if (betUnits > MAX_BETUNITS) {
										throw MAX_BETUNITS_ERROR_MSG;
									}
								}
							}
						}
					}
				} else {
					for ( var i = 0, l1 = typeArr.length; i < l1; i++) {
						var typeEl = typeArr[i];
						if (typeEl.checked) {
							var type = PassType[typeEl.value];
							for ( var j = 0, l2 = type.passMatchs.length; j < l2; j++) {
								betUnits += simpleCountFn(unDanBets, type.passMatchs[j]);
								if (betUnits > MAX_BETUNITS) {
									throw MAX_BETUNITS_ERROR_MSG;
								}
							}
						}
					}
				}
			} catch (e) {
				if (e == MAX_BETUNITS_ERROR_MSG) {
					updateBetUnits(e);
					return;
				}
			}
		}
		updateBetUnits(betUnits);
	}

	/**
	 * 缓存注数的计算结果
	 * 
	 * @param key 缓存的键
	 * @param result 缓存的值
	 * @return 缓存的值
	 */
	function cacheCountResult(key, result) {
		var cacheOption;
		if (result > 5000) {
			cacheOption = {
				expirationSliding : 1200,
				priority : CachePriority.High
			};
		} else if (result > 1000) {
			cacheOption = {
				expirationSliding : 600,
				priority : CachePriority.Normal
			};
		} else {
			cacheOption = {
				expirationSliding : 300,
				priority : CachePriority.Low
			};
		}
		countCache.setItem(key, result, cacheOption);
		return result;
	}

	/**
	 * 有设胆的注数计算
	 * 
	 * @param danBets 设胆的数组
	 * @param unDanBets 非设胆的数组
	 * @param danHit 胆码对几场
	 * @param needs 需要的场次数目（过几关）
	 * @return 注数
	 */
	function shedanCountFn(danBets, unDanBets, danHit, needs) {
		var key = "C3_" + danBets + "_" + unDanBets + "_" + danHit + "_" + needs;// 用于缓存计算结果的KEY
		var rs = countCache.getItem(key);// 从 缓存查找是否已经有计算结果

		if (rs == null) {// 无缓存，进行计算
			var count = 0;
			C3(danBets.length, danHit, function(comb, n, m) {
				var c = 1;// 胆码的组合计算结果
					var pos = 0;
					for ( var i = 0; i < n; i++) {
						if (comb[i] == true) {
							c *= danBets[i];
							if (c > MAX_BETUNITS) {
								throw MAX_BETUNITS_ERROR_MSG;
							}
							pos++;
							if (pos == m)
								break;
						}
					}

					if (m < needs) {// 需要拖码
						var c2 = simpleCountFn(unDanBets, needs - m);
						count += c * c2;
					} else {// 不需要拖码
						count += c;
					}
					if (count > MAX_BETUNITS) {
						throw MAX_BETUNITS_ERROR_MSG;
					}
				});

			rs = count;
			cacheCountResult(key, rs);// 缓存计算结果
		}

		return rs;
	}

	/**
	 * 无设胆的注数计算
	 * 
	 * @param bets 可选数组
	 * @param needs 需要的场次数目（过几关）
	 * @return 注数
	 */
	function simpleCountFn(bets, needs) {
		var key = "C3_" + bets + "_" + needs;// 用于 缓存计算结果的KEY
		var rs = countCache.getItem(key);// 从 缓存查找是否已经有计算结果

		if (rs == null) {
			var count = 0;
			C3(bets.length, needs, function(comb, n, m) {
				var c = 1;
				var pos = 0;
				for ( var i = 0; i < n; i++) {
					if (comb[i] == true) {
						c *= bets[i];
						if (c > MAX_BETUNITS) {
							throw MAX_BETUNITS_ERROR_MSG;
						}
						pos++;
						if (pos == m)
							break;
					}
				}
				count += c;
				if (count > MAX_BETUNITS) {
					throw MAX_BETUNITS_ERROR_MSG;
				}
			});

			rs = count;
			cacheCountResult(key, rs);// 缓存计算结果
		}

		return rs;
	}
	
	window.prizeForecast = function(){
		var createForm = getCreateForm();
		var units = createForm.elements['createForm.units'].value;
		if (units == 0) {
			alert("注数不能等于0！");
			return false;
		}
		var formActionUrl = createForm.action;
		var formTarget = createForm.target;
		createForm.action = formActionUrl.replace(/![a-zA-Z]+/ig, '!prizeForecastOfInit');
		createForm.target = '_blank';
		var options = {
				type : 'POST',
				cache : false,
				data : {
					ajax : 'true'
				},
				success : function(data, textStatus) {
					 $.weeboxs.open(data, {title:'奖金预测', width:750});
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert('提交请求失败.');
				}
		};
		$(createForm).ajaxSubmit(options);
		createForm.action = formActionUrl;
		createForm.target = formTarget;
	};
	/**
	 * 更改购买方式
	 * 
	 * @param obj
	 */
	window.compoundChgShareType = function(obj) {
		var createForm = getCreateForm();
		var tr_TOGETHER_title = document.getElementById('tr_TOGETHER_title');
		var tr_TOGETHER_content = document.getElementById('tr_TOGETHER_content');
		var schemeCost = createForm.elements['createForm.schemeCost'].value;
		if (obj.value == 'TOGETHER') {
			if(schemeCost!=0){
				createForm.elements['createForm.subscriptionCost'].value=schemeCost/2;
				updateSubscriptionPer();
			}
			tr_TOGETHER_title.style.display = '';
			tr_TOGETHER_content.style.display = '';
		} else {
			tr_TOGETHER_title.style.display = 'none';
			tr_TOGETHER_content.style.display = 'none';
		}
	};
});